﻿using Core.SimpleTemp.Application.Common;
using Core.SimpleTemp.Application.ServiceApp.SysApp.FileApp;
using Core.SimpleTemp.Common;
using Core.SimpleTemp.Common.BaseLibrary;
using Core.SimpleTemp.Entitys;
using Core.SimpleTemp.Entitys.Sys;
using Core.SimpleTemp.Repositories.RepositoryEntityFrameworkCore.Sys;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Options;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;

namespace Core.SimpleTemp.Application.UserApp
{

    [AutoDi()]
    public class SysFileAppService : BaseAppService<SysFileDto, SysFile, SysFileRepository>
    {
        WebAppOptions _webAppOptions;
        PPT2Html _pPT2Html;
        public SysFileAppService(SysFileRepository repository, IOptionsMonitor<WebAppOptions> webAppOptions, PPT2Html pPT2Html) : base(repository)
        {
            _repository = repository;
            _webAppOptions = webAppOptions.CurrentValue;
            _pPT2Html = pPT2Html;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="updateFileDto"></param>
        /// <returns></returns>
        public async Task<List<SysFileDto>> UploadFileAsync(UpdateFileDto updateFileDto)
        {
            List<SysFileDto> dtoList = new List<SysFileDto>();
            if (!object.Equals(updateFileDto.Files, null) && updateFileDto.Files.Any())
            {
                foreach (var item in updateFileDto.Files)
                {
                    var dto = this.CreateSysFileDto(item, updateFileDto.B_ID);
                    await Filehelp.WriteFileAsync(item.OpenReadStream(), CreatePath(dto.FilePath));
                    var retDot = await InsertAsync(dto, false);
                    dtoList.Add(retDot);
                }
                await _repository.SaveAsync();
            }
            return dtoList;
        }


        public async Task<List<SysFileDto>> UploadFileForPXPlan(UpdateFileDto updateFileDto, string data)
        {
            List<SysFileDto> dtoList = new List<SysFileDto>();
            if (!object.Equals(updateFileDto.Files, null) && updateFileDto.Files.Any())
            {
                foreach (var item in updateFileDto.Files)
                {
                    var dto = this.CreateSysFileDto(item, updateFileDto.B_ID);
                    await Filehelp.WriteFileAsync(item.OpenReadStream(), CreatePath(dto.FilePath));
                    var retDot = await InsertAsync(dto, false);
                    dtoList.Add(retDot);

                    PX_SignDetail fj = new PX_SignDetail();
                    fj.SignID = Guid.Parse(data);
                    fj.DeptID = retDot.Id;
                    fj.CreateTime = DateTime.Now;
                    fj.Remark = retDot.FileName;

                    _repository._dbContext.PX_SignDetail.Add(fj);

                }
                await _repository.SaveAsync();
            }
            return dtoList;
        }

        /// <summary>
        /// 创建附件Dto
        /// </summary>
        /// <param name="formFile"></param>
        /// <param name="B_ID"></param>
        /// <returns></returns>
        private SysFileDto CreateSysFileDto(IFormFile formFile, Guid? B_ID)
        {
            var path = Filehelp.GetFilePath(_webAppOptions.FileSaveBasePath);
            return new SysFileDto
            {
                B_ID = B_ID ?? Guid.Empty,
                FileName = formFile.FileName,
                FilePath = path.Replace(_webAppOptions.FileSaveBasePath, ""),
                Extension = System.IO.Path.GetExtension(formFile.FileName),
                Size = formFile.Length,
                Type = "UUID",
                UDate = DateTime.Now,
                IDate = DateTime.Now
            };
        }

        public string CreatePath(string path)
        {
            return _webAppOptions.FileSaveBasePath + path;
        }

        /// <summary>
        /// ppt上传,一个文件保存两份 ppt&html  html为在线浏览准备
        /// </summary>
        /// <param name="updateFileDto"></param>
        /// <returns></returns>
        public async Task<List<SysFileDto>> UploadPPTAsync(UpdateFileDto updateFileDto)
        {

            List<SysFileDto> dtoList = new List<SysFileDto>();
            if (!object.Equals(updateFileDto.Files, null) && updateFileDto.Files.Any())
            {
                foreach (var item in updateFileDto.Files)
                {
                    //保存上传文件到磁盘
                    var pptDto = this.CreateSysFileDto(item, updateFileDto.B_ID);
                    await Filehelp.WriteFileAsync(item.OpenReadStream(), CreatePath(pptDto.FilePath));
                    //ppt转成html
                    var htmlDto = new SysFileDto
                    {
                        B_ID = updateFileDto?.B_ID ?? Guid.Empty,
                        FileName = System.IO.Path.GetFileNameWithoutExtension(item.FileName) + ".html",
                        FilePath = pptDto.FilePath + ".html",
                        Extension = ".html",
                        Size = 0,
                        Type = "UUID",
                        UDate = DateTime.Now,
                        IDate = DateTime.Now
                    };
                    var htmlPath = _pPT2Html.ToHtml(CreatePath(pptDto.FilePath), CreatePath(htmlDto.FilePath));//ppt 转html
                    //向附件表插入数据
                    pptDto = await InsertAsync(pptDto, false);
                    htmlDto = await InsertAsync(htmlDto, false);
                    dtoList.Add(pptDto);
                    dtoList.Add(htmlDto);
                }
                await _repository.SaveAsync();
            }
            return dtoList;
        }
    }
}